
import re
import sys

class MapMysql:
  
  def __init__(self):
    pass
  

  def getMD(self, coltype):
    
    coltype = coltype.lower()
    
    ttype  = coltype.split('(')
    
    rawstr = r"""\((.*)\)"""
    compile_obj = re.compile(rawstr)
    match_obj = compile_obj.search(coltype)
    length = 0
    if match_obj:
      all_groups = match_obj.groups()
      length = match_obj.group(1)
      if length.find(','):
        lth = length.split(',')
        length = lth[0]
    
    if len(ttype) == 1:
      dtype = coltype
    else:
      dtype = ttype[0]      
    
    if dtype == 'bigint':  
      jtype = 'java.lang.Long'
      stype = 'Long'
      tvalue = '10L'

    elif dtype == 'integer':
      jtype = 'java.lang.Integer'
      stype = 'Integer'
      tvalue = '9'


    elif dtype == 'int':
      jtype = 'java.lang.Integer'
      stype = 'Integer'
      tvalue = '9'
    elif dtype == 'varchar':
      jtype = 'java.lang.String'
      stype = 'String'
      tvalue = '"test"'

    elif dtype == 'datetime':  
      jtype = 'java.util.Date'
      stype = 'Date'
      tvalue = 'new Date()'
    elif dtype == 'date':  
      jtype = 'java.util.Date' 
      stype = 'Date' 
      tvalue = 'new Date()'
    elif dtype == 'text':  
      jtype = 'java.lang.String'      
      stype = 'String'
      tvalue = '"test again"' 
    elif dtype == 'tinyint':  
      jtype = 'java.lang.Short'
      stype = 'Short'
      tvalue = '1'
    elif dtype == 'numeric':  
      jtype = 'java.lang.Double'      
      stype = 'Double'      
      tvalue = '10.26'
    elif dtype == 'decimal':  
      jtype = 'java.lang.Double'      
      stype = 'Double'      
      tvalue = '10.26'
    else:
      print dtype
      print "============================================"
      print "in mapmysql [76] please add new type"
      raise "error"
      sys.exit(0)
    typedict= {}
    typedict['longtype'] = jtype
    typedict['shorttype'] = stype
    typedict['length'] = length
    typedict['value'] = tvalue
    
    #print coltype,jtype,stype
    
    return typedict
    #return jtype

# TODO, use as a hash 

if __name__ == "__main__":
  mm = Mmysql()
  print mm.getMD('int(11)')
